package com.konsung.ui.defineview;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.konsung.bean.QueryItem;
import com.konsung.healthfile.R;
import com.konsung.util.UiUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiangshicheng on 2018/12/5 0005.
 * 筛选弹出框
 */

public class SelectDialog extends Dialog implements DatePickerDialog.OnDateSetListener
        , View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private Context context;
    private OnQueryListener onQueryListener;
    private int sex = -1; // -1全部  1男 2女   默认选择全部
    private int upload = 0; // 0全部 1未上传 2已上传 默认选择全部
    private Date date = null;
    private Calendar calendar = null;
    private SimpleDateFormat sdf;
    DatePickerDialog datePickerDialog; //时间选择弹出框
    private int timeFlag; //时间选择框标识 1 测量时间起 2 测量时间止
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.rg_upload)
    RadioGroup rgIsUpload;
    @BindView(R.id.rg_sex)
    RadioGroup rgSex;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_stop_time)
    TextView tvStopTime;
    @BindView(R.id.btn_today)
    Button btnToday;
    @BindView(R.id.btn_month)
    Button btnMonth;
    @BindView(R.id.btn_week)
    Button btnWeek;
    @BindView(R.id.btn_clear)
    Button btnClear;
    @BindView(R.id.btn_query)
    Button btnQuery;
    /**
     * 构造方法
     * @param context 上下文引用
     * @param onQueryListener 接口回调
     */
    public SelectDialog(Context context, OnQueryListener onQueryListener) {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;
        this.onQueryListener = onQueryListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_patient_query);
        ButterKnife.bind(this);
        initData();
        initListener();
    }

    /**
     * 页面数据初始化
     */
    private void initData() {
        initDataTime();
        datePickerDialog = new DatePickerDialog(context, R.style.AppDate, this,
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        tvStopTime.setText(this.sdf.format(calendar.getTime()));
    }

    /**
     * 初始化时间
     */
    private void initDataTime() {
        sdf = UiUtils.getDateFormat(UiUtils.DateState.SHORT);
        date = new Date();
        calendar = Calendar.getInstance();
    }
    /**
     * 页面控件监听
     */
    private void initListener() {
        ivClose.setOnClickListener(this);
        tvStartTime.setOnClickListener(this);
        tvStopTime.setOnClickListener(this);
        btnToday.setOnClickListener(this);
        btnMonth.setOnClickListener(this);
        btnWeek.setOnClickListener(this);
        btnClear.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
        rgIsUpload.setOnCheckedChangeListener(this);
        rgSex.setOnCheckedChangeListener(this);
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
        calendar.set(year, monthOfYear, dayOfMonth);
        if (timeFlag == 1) {
            showView(tvStartTime);
        } else if (timeFlag == 2) {
            showView(tvStopTime);
        }
    }

    /**
     * 日期控件显示时间
     * @param tvTime 日期控件
     */
    private void showView(TextView tvTime) {
        tvTime.setText(this.sdf.format(calendar.getTime()));
        tvTime.setTextColor(UiUtils.getColor(R.color.title_color));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_start_time:
                datePickerDialog.show();
                timeFlag = 1;
                break;
            case R.id.tv_stop_time:
                datePickerDialog.show();
                timeFlag = 2;
                break;
            case R.id.iv_close:
                this.dismiss();
                break;
            case R.id.btn_today:
                //今天时间段
                initDataTime();
                calendar.setTime(date);
                tvStartTime.setText(this.sdf.format(calendar.getTime()));
                tvStopTime.setText(this.sdf.format(calendar.getTime()));
                break;
            case R.id.btn_month:
                //最近一月时间段
                initDataTime();
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, -1);
                tvStartTime.setText(sdf.format(calendar.getTime()));
                tvStopTime.setText(sdf.format(date));
                break;
            case R.id.btn_week:
                //最近一周时间段
                initDataTime();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_WEEK, -7);
                tvStartTime.setText(sdf.format(calendar.getTime()));
                tvStopTime.setText(sdf.format(date));
                break;
            case R.id.btn_clear:
                //清除筛选条件
                initDataTime();
                tvStartTime.setText(UiUtils.getString(R.string.measure_start_time));
                tvStopTime.setText(this.sdf.format(calendar.getTime()));
                tvStartTime.setTextColor(UiUtils.getColor(R.color.value_default));
                tvStopTime.setTextColor(UiUtils.getColor(R.color.value_default));
                rgIsUpload.check(R.id.rb_upload_all);
                rgSex.check(R.id.rb_all);
                if (onQueryListener != null) {
                    onQueryListener.clearSelection();
                }
                break;
            case R.id.btn_query:
                //筛选
                QueryItem queryItem = new QueryItem();
                queryItem.setUpload(upload);
                queryItem.setSex(sex);
                String start = tvStartTime.getText().toString();
                queryItem.setMeasureStart(start);
                String end = tvStopTime.getText().toString();
                queryItem.setMeasureEnd(end);
                queryItem.setQuery(false);
                if (onQueryListener != null) {
                    onQueryListener.onQuery(queryItem);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        switch (id) {
            //是否上传-全部
            case R.id.rb_upload_all:
                upload = 0;
                break;
            //是否上传-未上传
            case R.id.rb_not_upload:
                upload = 1;
                break;
            //是否上传-已上传
            case R.id.rb_uploaded:
                upload = 2;
                break;
            //性别-全部
            case R.id.rb_all:
                sex = -1;
                break;
            //性别-男
            case R.id.rb_man:
                sex = 1;
                break;
            //性别-女
            case R.id.rb_woman:
                sex = 2;
                break;
            default:
                break;
        }
    }

    /**
     * 筛选回调接口
     */
    public interface OnQueryListener {
        /**
         * 筛选回调
         *
         * @param item 筛选条件
         */
        void onQuery(QueryItem item);

        /**
         * 清空条件
         */
        void clearSelection();
    }
}
